package oppsa.util;

import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileNameExtensionFilter;

import oppsa.model.MysqlDump;

public class FileChooser extends JFrame {
	
	private JFileChooser c;
	private MysqlDump dump;
	
	  public FileChooser() {
	      c = new JFileChooser();
	      dump = new MysqlDump();
	  }
	  
	  /**
	   * openFile
	   * import a backup file
	   */
	  public File openFile(){
		  FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("backup files (*.backup)", "backup");
          c.setFileFilter(xmlfilter);
    	  int rVal = c.showOpenDialog(FileChooser.this);  
	      
	      if (rVal == JFileChooser.APPROVE_OPTION) {
	        
	        File source =  c.getSelectedFile();
	        
	        try {
				dump.importHSQLDB(source);
				return source;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	      }
	      return null;
	  }
	  
	  /**
	   * savefile
	   * export de script file on a backup file
	   */
	  public void saveFile(){
		  FileNameExtensionFilter xmlfilter = new FileNameExtensionFilter("backup files (*.backup)", "backup");
          c.setFileFilter(xmlfilter);
    	  int rVal = c.showSaveDialog(FileChooser.this);
      
	      if (rVal == JFileChooser.APPROVE_OPTION) {
	        
	        File dest = new File (c.getCurrentDirectory().toString()+"\\"+c.getSelectedFile().getName()+".backup");
	        try {
				dump.exportHSQLDB(dest);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	      }
	      if (rVal == JFileChooser.CANCEL_OPTION) {
	      }
	  }
	}
